package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 员工管理模块
 */
@RestController
@RequestMapping("employee")
@Slf4j
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    /**
     * 登录验证
     * @param employee 用户名 和 密码
     * @return 登录结果
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        log.info("登录信息:{}",employee);
        /*根据用户名查询*/
        /*LambdaQueryWrapper<Employee> wrapper = Wrappers.lambdaQuery(Employee.class)
                .eq(Employee::getUsername, employee.getUsername());
        Employee employee1 = employeeService.getOne(wrapper);*/
        Employee emp = employeeService.lambdaQuery().eq(Employee::getUsername, employee.getUsername())
                .one();
        /*判断用户是否存在*/
        if (emp == null){
            return R.error("账号错误");
        }
        /*判断密码是否正确*/
        //获取employee密码进行md5加密,在和数据库对比
        String password = employee.getPassword();
        /*使用md5加密*/
        String md5Hex = DigestUtils.md5Hex(password);
        /*判断密码 ,将加密的密码和数据库判断*/
        if ( !StringUtils.equals( md5Hex, emp.getPassword())){
            /*密码错误*/
            return R.error("密码错误!");
        }
       /* 3.判断账号状态码 */
        if(emp.getStatus() == 0){
            return R.error("该账号已禁用");
        }
        /* 4.登陆成功 ,将员工id 记入 session中 */
        session.setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 退出系统
     * @param session session域对象中员工id
     * @return 返回标识
     */
    @PostMapping("logout")
    public R<String> Logout(HttpSession session){
        log.info("用户:{} 退出系统", session.getAttribute("employee"));
        //销毁session
        session.invalidate();
        //返回code=1
        return R.success("成功退出");
    }

    /**
     * 添加员工 、修改员工信息
     * @param employee 员工信息
     * @param session 当前登录的id
     * @return 返回操作结果
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpSession session){
        log.info("员工信息: "+employee.toString());
        //设置默认初始密码"123456" ,需要md5加密
        employee.setPassword(DigestUtils.md5Hex("123456"));

        boolean save = employeeService.save(employee);
        if (save){
            return R.success("添加员工成功");
        }else{
            return R.error("添加失败");
        }
    }

    /**
     * 员工信息分页查询
     * @param page 页数
     * @param pageSize 每页条数
     * @param name 根据名称搜索
     * @return 查询结果
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //请求数据
        log.info("请求数据:页数:{} 每页条数:{} 搜索条件:{}",page,pageSize,name);
        //创建分页查询构造器
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        //查询条件
        // StringUtils.isNotEmpty(name) 判断name是否为空,
        LambdaQueryWrapper<Employee> wrapper = Wrappers.lambdaQuery(Employee.class)
                .like(StringUtils.isNotEmpty(name), Employee::getName, name)
                .orderByDesc(Employee::getUpdateTime);
        //执行分页查询
        employeeService.page(pageInfo,wrapper);

        return R.success(pageInfo);
    }

    /**
     * 修改员工状态
     * @param employee 需要修改的员工信息
     * @return 修改结果
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        log.info("修改信息:" + employee);

        employeeService.updateById(employee);
        return R.success("修改员工状态成功");
    }

    /**
     * 修改员工信息，数据回显，查询
     * @param id 根据id查询
     * @return 返回员工信息
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("根据id查询。。.");
        Employee emp = employeeService.getById(id);
        return R.success(emp);
    }
}




